package com.zimmor.mq.rocket_train1.order;

import com.zimmor.mq.RocketMQProperties;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;

import java.util.List;

/**
 * @Author:zimmor
 * @Date: 2023/2/16 16:35
 * @Description 消费顺序消息
 * @version: 1.0.0
 */

public class Consumer {
    public static void main(String[] args) throws MQClientException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer-group1");
        consumer.setNamesrvAddr(RocketMQProperties.NAME_SRV_ADDR);
        consumer.setMessageModel(MessageModel.CLUSTERING);
        consumer.subscribe("order", "tag");
        consumer.registerMessageListener(new MessageListenerOrderly() {
            @Override
            public ConsumeOrderlyStatus consumeMessage(List<MessageExt> list, ConsumeOrderlyContext consumeOrderlyContext) {
                for (MessageExt messageExt : list) {
                    System.out.println("msg:" + new String(messageExt.getBody()) + "  orderId:" + messageExt.getKeys());
                }
                return ConsumeOrderlyStatus.SUCCESS;
            }
        });
        consumer.start();
    }
}
